package cn.wen.yinxiu.system.service.impl;

import cn.wen.yinxiu.common.enums.DelEnum;
import cn.wen.yinxiu.common.enums.StatusEnum;
import cn.wen.yinxiu.common.to.system.NoticeTO;
import cn.wen.yinxiu.common.to.user.UserTO;
import cn.wen.yinxiu.common.to.works.SimpleWorksTO;
import cn.wen.yinxiu.system.feign.UserFeignService;
import cn.wen.yinxiu.system.feign.WorksFeignService;
import cn.wen.yinxiu.system.vo.NoticeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.wen.yinxiu.common.toolkit.PageUtils;
import cn.wen.yinxiu.common.toolkit.Query;

import cn.wen.yinxiu.system.dao.NoticesDao;
import cn.wen.yinxiu.system.entity.NoticesEntity;
import cn.wen.yinxiu.system.service.NoticesService;


@Service("noticesService")
public class NoticesServiceImpl extends ServiceImpl<NoticesDao, NoticesEntity> implements NoticesService {

    @Value("${yinxiu.notice.pageSize}")
    private Integer pageSize;

    @Autowired
    private NoticesDao noticesDao;

    @Autowired
    private UserFeignService userFeignService;

    @Autowired
    private WorksFeignService worksFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<NoticesEntity> page = this.page(
                new Query<NoticesEntity>().getPage(params),
                new QueryWrapper<NoticesEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public NoticeTO saveNoticeByTo(NoticeTO notice) {
        NoticesEntity target = new NoticesEntity();
        BeanUtils.copyProperties(notice, target);
        target.setIsDeleted(DelEnum.NORMAL.code());
        target.setStatus(StatusEnum.NORMAL_USE.code());
        noticesDao.insert(target);
        notice.setNoticeId(target.getId());
        return notice;
    }

    @Override
    public List<NoticeVO> selectNoticeList(Long userId, Integer page) {
        QueryWrapper<NoticesEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("recipient_id", userId);
        queryWrapper.eq("status", StatusEnum.NORMAL_USE.code());
        queryWrapper.eq("is_deleted", DelEnum.NORMAL.code());
        queryWrapper.orderByDesc("create_time");
        queryWrapper.last("LIMIT " + pageSize * (page - 1) + "," + pageSize);
        List<NoticesEntity> noticeList = noticesDao.selectList(queryWrapper);
        List<NoticeVO> resList = noticeList.stream().map(item -> {
            NoticeVO target = new NoticeVO();
            BeanUtils.copyProperties(item, target);
            target.setNoticeId(item.getId());
            // 获取该用户信息
            UserTO user = userFeignService.getUserInfoById(item.getSenderId()).getData();
            target.setSenderName(user.getUsername());
            target.setLastLoginTime(user.getLastLoginTime());
            if (item.getSourceId() != null) {
                // 获取作品的信息
                SimpleWorksTO works = worksFeignService.getWorksSimpleInfo(item.getSourceId()).getData();
                if (works == null) {
                    target.setSourcePhoto("");
                } else {
                    target.setSourcePhoto(works.getCoverPath());
                }
            }
            return target;
        }).collect(Collectors.toList());
        return resList;
    }

    @Override
    public boolean deleteNoticeById(Long noticeId) {
        NoticesEntity target = new NoticesEntity();
        target.setId(noticeId);
        target.setIsDeleted(DelEnum.DELETE.code());
        return noticesDao.updateById(target) > 0;

    }

}